package com.xinhe.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.querydsl.core.QueryResults;
import com.xinhe.web.annotation.WebUsed;
import com.xinhe.web.common.Result;
import com.xinhe.web.entity.AdoptedTreeOrder;
import com.xinhe.web.response.AdoptedTreeOrderView;
import com.xinhe.web.selfpay.WeixinJsapiService;
import com.xinhe.web.service.AdoptedTreeOrderService;
import com.xinhe.web.util.HttpServletRequestUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.Date;

@Api(value = "认养树订单", tags = {"认养树订单"})
@RestController
@RequestMapping(value = {"/adoptedTreeOrder"}, produces = {"application/json;charset=UTF-8"})
public class AdoptedTreeOrderController {
    @Autowired
    AdoptedTreeOrderService adoptedTreeOrderService;
    @Autowired
    WeixinJsapiService weixinJsapiService;

    @WebUsed
    @ApiOperation(httpMethod = "POST", value = "管理后台保存认养树订单")
    @RequestMapping({"/savePlatformAdoptedTreeOrder"})
    public Result savePlatformAdoptedTreeOrder(@ApiParam(value = "认养树id") @RequestParam(required = true) Long adoptedTreeId,
                                               @ApiParam(value = "认养数量") @RequestParam(name = "amount",required = true) Integer amount,
                                               @ApiParam(value = "会员id") @RequestParam(name = "userId",required = true) Long userId,
                                               @ApiParam(value = "买家备注") @RequestParam(name = "buyerRemark",required = false) String buyerRemark) {
        return adoptedTreeOrderService.savePlatformAdoptedTreeOrder(adoptedTreeId,amount,userId,buyerRemark);
    }
    @ApiOperation(httpMethod = "POST", value = "保存认养树订单")
    @RequestMapping({"/saveAdoptedTreeOrder"})
    public Result saveAdoptedTreeOrder(@ApiParam(value = "认养树id") @RequestParam(required = true) Long adoptedTreeId,
                                       @ApiParam(value = "认养数量") @RequestParam(name = "amount",required = true) Integer amount,
                                       @ApiParam(value = "会员id") @RequestParam(name = "userId",required = true) Long userId,
                                       @ApiParam(value = "买家备注") @RequestParam(name = "buyerRemark",required = false) String buyerRemark) {
        return adoptedTreeOrderService.saveAdoptedTreeOrder(adoptedTreeId,amount,userId,buyerRemark);
    }

    @WebUsed
    @ApiOperation(httpMethod = "GET", value = "分页查询认养树订单，即会员认养明细")
    @RequestMapping({"/pageAdoptedTreeOrder"})
    public QueryResults<AdoptedTreeOrderView> pageAdoptedTreeOrder(Long userId,Long adoptedTreeId,String status,String code,String keyword,
                                                                   Date startTime,Date endTime, int pageNum, int pageSize) {
        return this.adoptedTreeOrderService.pageAdoptedTreeOrder(userId,adoptedTreeId,status,code,keyword,
                 startTime,endTime,pageNum,pageSize);
    }

    @WebUsed
    @ApiOperation(httpMethod = "GET", value = "后台额外赠送云茶籽")
    @PostMapping({"/updateSeedYunChaSeed"})
    public Result updateSeedYunChaSeed(Long orderId, BigDecimal additionalYunChaSeed) {
        return this.adoptedTreeOrderService.updateSeedYunChaSeed(orderId, additionalYunChaSeed);
    }

    @ApiOperation(httpMethod = "GET", value = "后台取消额外赠送云茶籽")
    @PostMapping({"/updateCancelSeedYunChaSeed"})
    public Result updateCancelSeedYunChaSeed(Long orderId, BigDecimal additionalYunChaSeed) {
        return this.adoptedTreeOrderService.updateCancelSeedYunChaSeed(orderId, additionalYunChaSeed);
    }

    @ApiOperation(httpMethod = "POST", value = "小程序JSAPI支付")
    @PostMapping({"/requestJsapiPayParam"})
    public Result requestJsapiPayParam(@RequestParam Long orderId, String openId) {
        return this.adoptedTreeOrderService.requestJsapiPayParam(orderId, openId);
    }

    @ApiOperation(httpMethod = "POST", value = "小程序JSAPI支付回调", hidden = true)
    @PostMapping({"/payCallback"})
    public ResponseEntity.BodyBuilder payCallback(HttpServletRequest request, HttpServletResponse response) {
        //wechat_pay_notification.json
        //NotificationParserTest
        System.out.println("小程序JSAPI支付回调了");
        //应答给微信
        return this.weixinJsapiService.payCallback(request,response);
    }

    @ApiOperation(httpMethod = "POST", value = "小程序JSAPI支付回调", hidden = true)
    @RequestMapping({"/responseJsapiPay"})
    public Result responseJsapiPay() {
        System.out.println("小程序JSAPI支付回调了");
        return this.adoptedTreeOrderService.responseJsapiPay();
    }

    @ApiOperation(httpMethod = "GET", value = "分页查询认养树订单(用户)")
    @RequestMapping({"/pageAdoptedTreeOrderByUser"})
    public QueryResults<AdoptedTreeOrderView> pageAdoptedTreeOrderByUser(Long userId,Long adoptedTreeId,String code,
                                                                         int pageNum, int pageSize) {
        return this.adoptedTreeOrderService.pageAdoptedTreeOrderByUser(userId,adoptedTreeId,code,pageNum,pageSize);
    }

    @ApiOperation(httpMethod = "POST", value = "认养数量")
    @RequestMapping({"/adoptedTreeCount"})
    public Result adoptedTreeCount() {
        return this.adoptedTreeOrderService.adoptedTreeCount();
    }

    @ApiOperation(httpMethod = "POST", value = "余额支付")
    @RequestMapping({"/requestBalancePay"})
    public Result requestBalancePay(Long orderId) {
        return this.adoptedTreeOrderService.requestBalancePay(orderId);
    }

    @ApiOperation(httpMethod = "GET", value = "删除认养树订单")
    @ApiImplicitParam(name = "adoptedTreeOrderId", value = "标识", dataTypeClass = Long.class, required = true)
    @RequestMapping({"/deleteAdoptedTreeOrder"})
    public Result deleteAdoptedTreeOrder(Long adoptedTreeOrderId) {
        return this.adoptedTreeOrderService.delete(adoptedTreeOrderId);
    }

    @ApiOperation(httpMethod = "GET", value = "认养树订单详情")
    @ApiImplicitParam(name = "adoptedTreeOrderId", value = "标识", dataTypeClass = Long.class, required = true)
    @RequestMapping({"/detailAdoptedTreeOrder"})
    public Result detailAdoptedTreeOrder(Long adoptedTreeOrderId) {
        return this.adoptedTreeOrderService.findById(adoptedTreeOrderId);
    }

    @ApiOperation(httpMethod = "GET", value = "修改后台线下支付已确认")
    @RequestMapping({"/updateOfflinePay"})
    public Result updateOfflinePay(Long adoptedTreeOrderId) {
        return this.adoptedTreeOrderService.updateOfflinePay(adoptedTreeOrderId);
    }

}
